$(function () {

	function _operation_field_formatter(cellValue , options , rowData) {
		var mode = 'view';
		var buttons = [];
		var id = rowData['uid'];
		buttons.push('<div class="grid-operation-button">')

		if (rowData.state == -2) {
			buttons.push(str_format('<a onclick="setRoomState('+ rowData.uid +',0,this)" msg="将解除禁止，主播可以重新开始直播，您确认做此操作吗 ?" class="label label-success" href="javascript:;">恢复直播</a>', rowData['uid']));
		} else {
			buttons.push(str_format('<a onclick="setRoomState('+ rowData.uid +',-2,this)" msg="将禁止直播( <font color=red>如果主播当前正在直播则中断直播，并且禁止以后其开启直播推流</font> )，主播可以重新开始直播，您确认做此操作吗 ?" class="label label-warning" href="javascript:;">禁止直播</a>', rowData['uid']));
		}

		if (mode == 'view') {
			buttons.push(str_format('<a class="label label-info" href="/modules/live/livesnapshot.html?stream_id={0}">截图</a>', rowData['uid']));
			buttons.push(str_format('<a class="label label-danger" href="/modules/live/livevideo.html?stream_id={0}">录像</a>', rowData['uid']));
		}
		buttons.push('</div>');
		return buttons.join("&nbsp;")
	}

    $("#jqGrid").jqGrid({
        url: baseURL + 'dataview/viewliveroomlist/list',
        datatype: "json",
        colModel: [			
			{ label: '用户编号', name: 'uid', index: 'uid' , align : 'center', width:60, key: true },
			{ label: '直播室', name: 'liveRoundId', index: 'liveRoundId', width: 250 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/nickname')},
			{ label: '主播信息', name: 'avanta', width: 80 ,formatter : set_images_formatter},
			{ label: '昵称', name: 'nickname',  width: 80 },
			//{ label: '是否推荐 数字越高，推荐级别越高', name: 'rank', index: 'rank', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/rank')},
			//{ label: '直播室标题', name: 'title', index: 'title', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/title')},
			//{ label: '直播室封面', name: 'coverImage', index: 'cover_image', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/coverImage')},
			//{ label: '直播室密码', name: 'password', index: 'password', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/password')},
			//{ label: '人气', name: 'popCount', index: 'pop_count', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/popCount')},
			{ label: '在线数/礼物数', name: 'giftCount', index: 'gift_count', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/giftCount')},
			//{ label: '在线人数', name: 'onlineCount', index: 'online_count', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/onlineCount')},
			{ label: '累计直播', name: 'totalSeconds', index: 'total_seconds', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/totalSeconds')},
			{ label: '直播状态', name: 'state', index: 'state', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/state')},
			{ label: '最近直播时间', name: 'startDate', index: 'start_date', width: 80 ,formatter : timestampForMS},
			//{ label: '最近直播结束时间', name: 'endDate', index: 'end_date', width: 80 ,formatter : timestampForMS},
			//{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/mobile')},
			//{ label: '公会', name: 'guildId', index: 'guild_id', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/guildId')},
			//{ label: '已认证=1,未认证=0', name: 'isVerifyIdcard', index: 'is_verify_idcard', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/isVerifyIdcard')},
			//{ label: '真实姓名', name: 'realname', index: 'realname', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/realname')},

			//{ label: '性别，男=1,女=0', name: 'gender', index: 'gender', width: 80 ,formatter : get_grid_column_formatter('dataview/viewliveroomlist/gender')},
			{ label: '操作', name: '_operation_field', index: '_operation_field', formatter:_operation_field_formatter}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: false,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function() {
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});

			/*
			var columnArray = $('#jqGrid').jqGrid('getGridParam','colModel');
			$.each(columnArray,function (k,v){
				console.log(JSON.stringify(v))
			});
			var aTr = $('#jqGrid').find('tbody tr.jqgrow');
			for (var i = 0; i < aTr.length; i++) {
				var tr = $(aTr[i]);
				var item = tr.children();
				console.log(JSON.stringify([columnArray[i],tr.text()]))
				for (var j = 0; j < item.length; j++) {
					$(item[j]).attr('title', '')
				}
			}*/
		}
    });
});

var vm  = T.vm = new Vue({
	el:'#webpage',
	data:{
		appearance : {
			showList: true,
			showControllBar : true,
			showSearchForm : true
		},
		options: [{
			value: '',
			label: '全部'
		}, {
			value: '-2',
			label: '禁播'
		}, {
			value: '0',
			label: '未开播'
		}, {
			value: '1',
			label: '直播中'
		}],
		q:{
			nickname:'',
			state:''
		},
		live_type:"all_live",
		title: null,
		viewLiveRoomList: {}
	},
	methods: {
		changeLiveType:function(val){
			if(val=="today_live"){
				vm.q.live_type=val
			}else{
				vm.q.live_type=''
			}
			vm.reload();
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.appearance.showList = false;
			vm.title = "新增";
			vm.viewLiveRoomList = {};
		},
		update: function (targetId,event) {
			var uid = targetId || getSelectedRow();
			if(uid == null){
				return ;
			}
			vm.appearance.showList = false;
            vm.title = "修改";
            
            vm.getInfo(uid)
		},
		saveOrUpdate: function (event) {
			var url = vm.viewLiveRoomList.uid == null ? "dataview/viewliveroomlist/save" : "dataview/viewliveroomlist/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.viewLiveRoomList),
			    success: function(r){
			    	if(r.code > 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var uids = getSelectedRows();
			if(uids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dataview/viewliveroomlist/delete",
                    contentType: "application/json",
				    data: JSON.stringify(uids),
				    success: function(r){
						if(r.code > 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(uid){
			$.get(baseURL + "dataview/viewliveroomlist/info/"+uid, function(r){
                vm.viewLiveRoomList = r.viewLiveRoomList;
            });
		},
		reload: function (event) {
			vm.appearance.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		}
	}
});